package com.fr.util;

import com.fr.base.ArrayUtils;
import com.fr.base.BaseUtils;
import com.fr.base.DefaultValues;
import com.fr.base.FRContext;
import com.fr.report.core.UploadedImage;
import com.fr.report.script.core.parser.OperationUtils;
import java.awt.Color;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.PixelGrabber;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.SQLException;
import java.sql.Time;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Utils
{
  public static char[] FileNameInvalidChars = { '"', '\'', '[', ']', '\\', '/', ':', '*', '?', '<', '>', '|' };
  private static Properties prop = null;
  private static String[] availableFontFamilyNames4Report = null;

  public static void copy(InputStream paramInputStream, String paramString, File paramFile)
    throws IOException
  {
    File localFile = new File(paramFile, paramString);
    mkfile(localFile);
    FileOutputStream localFileOutputStream = new FileOutputStream(localFile);
    copyBinaryTo(paramInputStream, localFileOutputStream);
    paramInputStream.close();
    localFileOutputStream.close();
  }

  public static void copy(File paramFile1, File paramFile2)
    throws IOException
  {
    File localFile;
    File[] arrayOfFile;
    int i;
    if (paramFile1.isFile())
    {
      copy(new FileInputStream(paramFile1), paramFile1.getName(), paramFile2);
    }
    else if (paramFile1.isDirectory())
    {
      localFile = new File(paramFile2, paramFile1.getName());
      if (mkdirs(localFile))
      {
        arrayOfFile = paramFile1.listFiles();
        for (i = 0; i < arrayOfFile.length; ++i)
          copy(arrayOfFile[i], localFile);
      }
    }
  }

  public static String doubleToString(double paramDouble)
  {
    return convertNumberStringToString(new Double(paramDouble));
  }

  public static String convertNumberStringToString(Number paramNumber)
  {
    int k;
    if (OperationUtils.POSITIVE_INFINITY.equals(paramNumber))
      return "∞";
    if (OperationUtils.NEGATIVE_INFINITY.equals(paramNumber))
      return "-∞";
    String str1 = paramNumber.toString();
    int i = str1.indexOf(46);
    if (i < 0)
      return str1;
    int j = str1.indexOf(69);
    if ((i == 1) && (j > 0))
    {
      str2 = str1.substring(0, j);
      StringBuffer localStringBuffer1 = new StringBuffer(str2.substring(2));
      localStringBuffer1.insert(0, str2.charAt(0));
      String str3 = str1.substring(j + 1);
      if (str3.startsWith("+"))
        str3 = str3.substring(1);
      int l = Integer.parseInt(str3);
      if (l <= 0)
      {
        if (localStringBuffer1.charAt(localStringBuffer1.length() - 1) == '0')
          localStringBuffer1.deleteCharAt(localStringBuffer1.length() - 1);
        for (int i1 = 0; i1 < -l; ++i1)
          localStringBuffer1.insert(0, "0");
        localStringBuffer1.insert(1, ".");
      }
      else if (localStringBuffer1.length() > l + 1)
      {
        localStringBuffer1.insert(l + 1, '.');
      }
      else
      {
        while (localStringBuffer1.length() < l + 1)
          localStringBuffer1.append("0");
      }
      return localStringBuffer1.toString();
    }
    String str2 = str1.substring(i + 1);
    if ((k = str2.indexOf("9999")) >= 0)
    {
      double d = Double.parseDouble(str1);
      if (k == 0)
        return Long.toString(Math.round(d));
      if (Math.abs(d) > 9.223372036854776E+018D)
      {
        str4 = Double.toString(d);
        int i2 = str4.indexOf(".");
        if ((i2 > 0) && (str4.length() - i2 - 1 > k))
          str4 = str4.substring(0, i2 + k + 1);
        if (str4.endsWith(".0"))
          return str4.substring(0, str4.length() - 2);
        return str4;
      }
      String str4 = (d < 0.0D) ? "-" : "";
      long l1 = ()Math.pow(10.0D, k - 1);
      long l2 = l1 * 10L;
      d = Math.abs(d);
      long l3 = Math.round(d * l2);
      String str5 = str4 + Long.toString(l3 / l2);
      long l4 = (l2 == 0L) ? 0L : l3 % l2;
      if (l4 < 0L)
        return str1;
      if (l4 == 0L)
      {
        localStringBuffer2 = new StringBuffer("");
        for (int i3 = k; i3 > 0; --i3)
          localStringBuffer2.insert(0, "0");
        return str5 + "." + localStringBuffer2;
      }
      StringBuffer localStringBuffer2 = new StringBuffer(Long.toString(l4));
      while (l4 < l1)
      {
        l4 *= 10L;
        localStringBuffer2.insert(0, "0");
      }
      return str5 + "." + localStringBuffer2;
    }
    if ((k = str2.indexOf("0000")) >= 0)
      str1 = str1.substring(0, i + Math.max(k, 1) + 1);
    if (str1.endsWith(".0"))
      str1 = str1.substring(0, str1.length() - 2);
    return str1;
  }

  public static Object blob2Object(Blob paramBlob, boolean paramBoolean)
  {
    if (paramBoolean);
    try
    {
      return BaseUtils.readImage(paramBlob.getBinaryStream());
    }
    catch (Exception localSQLException)
    {
      FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
      break label57:
      try
      {
        return inputStream2Object(paramBlob.getBinaryStream());
      }
      catch (SQLException localSQLException)
      {
        FRContext.getLogger().log(Level.WARNING, localSQLException.getMessage(), localSQLException);
      }
    }
    label57: return null;
  }

  public static String clob2String(Clob paramClob)
  {
    if (paramClob == null)
      return "";
    try
    {
      return reader2String(paramClob.getCharacterStream());
    }
    catch (SQLException localSQLException)
    {
      FRContext.getLogger().log(Level.WARNING, localSQLException.getMessage(), localSQLException);
    }
    return "";
  }

  public static Object inputStream2Object(InputStream paramInputStream)
  {
    BufferedImage localBufferedImage = null;
    try
    {
      localBufferedImage = BaseUtils.readImage(paramInputStream);
    }
    catch (IOException localIOException)
    {
      FRContext.getLogger().log(Level.WARNING, localIOException.getMessage(), localIOException);
    }
    if (localBufferedImage != null)
      return localBufferedImage;
    return inputStream2String(paramInputStream);
  }

  public static String reader2String(Reader paramReader)
  {
    if (paramReader == null)
      return "";
    StringWriter localStringWriter = new StringWriter();
    try
    {
      copyCharTo(paramReader, localStringWriter);
      StringBuffer localStringBuffer = localStringWriter.getBuffer();
      paramReader.close();
      localStringWriter.close();
      return localStringBuffer.toString();
    }
    catch (IOException localIOException1)
    {
      try
      {
        paramReader.close();
      }
      catch (IOException localIOException2)
      {
        FRContext.getLogger().log(Level.WARNING, localIOException2.getMessage(), localIOException2);
      }
      try
      {
        localStringWriter.close();
      }
      catch (IOException localIOException3)
      {
        FRContext.getLogger().log(Level.WARNING, localIOException3.getMessage(), localIOException3);
      }
    }
    return "";
  }

  public static String inputStream2String(InputStream paramInputStream)
  {
    BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(paramInputStream));
    StringBuffer localStringBuffer = new StringBuffer();
    try
    {
      String str = "";
      while ((str = localBufferedReader.readLine()) != null)
        localStringBuffer.append(str);
    }
    catch (IOException localIOException)
    {
      FRContext.getLogger().log(Level.WARNING, localIOException.getMessage(), localIOException);
    }
    return localStringBuffer.toString();
  }

  public static byte[] inputStream2Bytes(InputStream paramInputStream)
  {
    byte[] arrayOfByte1 = new byte[32768];
    byte[] arrayOfByte2 = new byte[0];
    try
    {
      while ((i = paramInputStream.read(arrayOfByte1)) > 0)
      {
        int i;
        byte[] arrayOfByte3;
        if (arrayOfByte1.length == i)
          arrayOfByte3 = arrayOfByte1;
        else
          arrayOfByte3 = ArrayUtils.subarray(arrayOfByte1, 0, i);
        arrayOfByte2 = ArrayUtils.addAll(arrayOfByte2, arrayOfByte3);
      }
    }
    catch (IOException localIOException1)
    {
      FRContext.getLogger().log(Level.WARNING, localIOException1.getMessage(), localIOException1);
    }
    finally
    {
      try
      {
        paramInputStream.close();
      }
      catch (IOException localIOException2)
      {
        FRContext.getLogger().log(Level.WARNING, localIOException2.getMessage(), localIOException2);
      }
    }
    return arrayOfByte2;
  }

  public static Number objectToNumber(Object paramObject, boolean paramBoolean)
  {
    if (paramObject == null)
      return new Integer(0);
    if (paramObject instanceof Number)
      return ((Number)paramObject);
    String str = objectToString(paramObject);
    Object localObject = string2Number(str);
    if ((localObject == null) && (!(paramBoolean)))
      localObject = new Integer(0);
    return ((Number)localObject);
  }

  public static Number string2Number(String paramString)
  {
    if (paramString == null)
      return null;
    if (paramString.startsWith("+"))
      paramString = paramString.substring(1, paramString.length());
    if ((paramString.startsWith("0")) && (paramString.length() > 1) && (!(paramString.startsWith("0E"))) && (!(paramString.startsWith("0."))))
      return null;
    if (paramString.matches("[\\-]?\\d+"))
      try
      {
        return Integer.valueOf(paramString);
      }
      catch (NumberFormatException localNumberFormatException1)
      {
        return new BigInteger(paramString);
      }
    if (paramString.matches("[\\-]?\\d+([\\.]\\d+)?([Ee][\\-\\+]?\\d+)?"))
    {
      try
      {
        Double localDouble = Double.valueOf(paramString);
        if (localDouble.toString().equals(paramString))
          return localDouble;
      }
      catch (NumberFormatException localNumberFormatException2)
      {
      }
      return new BigDecimal(paramString);
    }
    return null;
  }

  public static String objectToString(Object paramObject)
  {
    if (paramObject == null)
      paramObject = "";
    if (paramObject instanceof String)
      return ((String)paramObject);
    if (paramObject instanceof Number)
      return convertNumberStringToString((Number)paramObject);
    if (paramObject instanceof Date)
    {
      DefaultValues localDefaultValues = FRContext.getDefaultValues();
      if (paramObject instanceof Time)
        return localDefaultValues.getTimeFormat().format(paramObject);
      String str = localDefaultValues.getDateTimeFormat().format(paramObject);
      if (str.endsWith("00:00:00"))
        return str.substring(0, str.length() - 9);
      return str;
    }
    if ((paramObject instanceof Image) || (paramObject instanceof UploadedImage))
      return "";
    return paramObject.toString();
  }

  public static Color getXORColor(Color paramColor)
  {
    if (paramColor == null)
      return null;
    return new Color(255 - paramColor.getRed(), 255 - paramColor.getGreen(), 255 - paramColor.getBlue());
  }

  public static int filterRGB(int paramInt1, int paramInt2)
  {
    int i = (int)((0.3D * (paramInt1 >> 16 & 0xFF) + 0.59D * (paramInt1 >> 8 & 0xFF) + 0.11D * (paramInt1 & 0xFF)) / 3.0D);
    i = 255 - (255 - i) * (100 - paramInt2) / 100;
    if (i < 0)
      i = 0;
    if (i > 255)
      i = 255;
    return (paramInt1 & 0xFF000000 | i << 16 | i << 8 | i);
  }

  public static String[] splitString(String paramString, char paramChar)
  {
    return splitString(paramString, "" + paramChar);
  }

  public static String[] splitString(String paramString1, String paramString2)
  {
    if ((paramString1 == null) || (paramString1.length() == 0))
      return new String[0];
    return paramString1.split("\\Q" + paramString2 + "\\E");
  }

  public static int[] splitStringToIntArray(String paramString1, String paramString2)
  {
    String[] arrayOfString = splitString(paramString1, paramString2);
    int[] arrayOfInt = new int[arrayOfString.length];
    for (int i = 0; i < arrayOfString.length; ++i)
      try
      {
        arrayOfInt[i] = Integer.parseInt(arrayOfString[i]);
      }
      catch (NumberFormatException localNumberFormatException)
      {
      }
    return arrayOfInt;
  }

  public static void swap(List paramList, int paramInt1, int paramInt2)
  {
    Object localObject = paramList.get(paramInt1);
    paramList.set(paramInt1, paramList.get(paramInt2));
    paramList.set(paramInt2, localObject);
  }

  public static String replaceAllString(String paramString, String[] paramArrayOfString1, String[] paramArrayOfString2)
  {
    for (int i = 0; i < paramArrayOfString1.length; ++i)
      paramString = replaceAllString(paramString, paramArrayOfString1[i], paramArrayOfString2[i]);
    return paramString;
  }

  public static String replaceAllString(String paramString1, String paramString2, String paramString3)
  {
    if (paramString1 == null)
      return null;
    return paramString1.replaceAll("\\Q" + paramString2 + "\\E", filterString(paramString3));
  }

  private static String filterString(String paramString)
  {
    if (paramString.indexOf(36) == -1)
      return paramString;
    StringBuffer localStringBuffer = new StringBuffer();
    for (int i = 0; i < paramString.length(); ++i)
    {
      char c = paramString.charAt(i);
      if (c == '$')
        localStringBuffer.append("\\$");
      else
        localStringBuffer.append(c);
    }
    return localStringBuffer.toString();
  }

  public static void copyBinaryTo(File paramFile, OutputStream paramOutputStream)
    throws IOException
  {
    copyBinaryTo(new FileInputStream(paramFile), paramOutputStream);
  }

  public static void copyBinaryTo(InputStream paramInputStream, OutputStream paramOutputStream)
    throws IOException
  {
    byte[] arrayOfByte = new byte[10240];
    while ((i = paramInputStream.read(arrayOfByte)) >= 0)
    {
      int i;
      paramOutputStream.write(arrayOfByte, 0, i);
    }
    paramOutputStream.flush();
  }

  public static void copyCharTo(Reader paramReader, Writer paramWriter)
    throws IOException
  {
    char[] arrayOfChar = new char[32768];
    while ((i = paramReader.read(arrayOfChar)) != -1)
    {
      int i;
      paramWriter.write(arrayOfChar, 0, i);
    }
    paramWriter.flush();
  }

  public static int[] grabImagePixelsArray(Image paramImage, int paramInt1, int paramInt2)
  {
    int[] arrayOfInt = new int[paramInt1 * paramInt2];
    PixelGrabber localPixelGrabber = new PixelGrabber(paramImage, 0, 0, paramInt1, paramInt2, arrayOfInt, 0, paramInt1);
    try
    {
      localPixelGrabber.grabPixels();
    }
    catch (InterruptedException localInterruptedException)
    {
      FRContext.getLogger().log(Level.WARNING, localInterruptedException.getMessage(), localInterruptedException);
    }
    return arrayOfInt;
  }

  public static boolean mkdirs(File paramFile)
  {
    if (!(paramFile.exists()))
      return paramFile.mkdirs();
    return true;
  }

  public static boolean mkfile(File paramFile)
    throws IOException
  {
    if (paramFile == null)
      return false;
    if (paramFile.exists())
      return true;
    mkdirs(paramFile.getParentFile());
    paramFile.createNewFile();
    return true;
  }

  public static String javaColorToCSSColor(Color paramColor)
  {
    StringBuffer localStringBuffer = new StringBuffer("rgb(");
    localStringBuffer.append(paramColor.getRed());
    localStringBuffer.append(",");
    localStringBuffer.append(paramColor.getGreen());
    localStringBuffer.append(",");
    localStringBuffer.append(paramColor.getBlue());
    localStringBuffer.append(")");
    return localStringBuffer.toString();
  }

  public static boolean isFileNameInvalidChar(char paramChar)
  {
    for (int i = 0; i < FileNameInvalidChars.length; ++i)
      if (paramChar == FileNameInvalidChars[i])
        return true;
    return false;
  }

  public static boolean deleteFile(File paramFile)
  {
    File[] arrayOfFile;
    int j;
    if ((paramFile == null) || (!(paramFile.exists())))
      return false;
    int i = 1;
    if (paramFile.isDirectory())
    {
      arrayOfFile = paramFile.listFiles();
      for (j = 0; j < arrayOfFile.length; ++j)
        if ((!(deleteFile(arrayOfFile[j]))) && (i != 0))
          i = 0;
    }
    i = ((paramFile.delete()) && (i != 0)) ? 1 : 0;
    return i;
  }

  public static String createWhiteSpaceString(int paramInt)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    for (int i = 0; i < paramInt; ++i)
      localStringBuffer.append(" ");
    return localStringBuffer.toString();
  }

  public static Method getDeclaredMethod(Class paramClass, String paramString, Class[] paramArrayOfClass)
  {
    try
    {
      return paramClass.getDeclaredMethod(paramString, paramArrayOfClass);
    }
    catch (NoSuchMethodException localNoSuchMethodException)
    {
      paramClass = paramClass.getSuperclass();
      if (paramClass != null)
        return getDeclaredMethod(paramClass, paramString, paramArrayOfClass);
    }
    return null;
  }

  public static Field getDeclaredField(Class paramClass, String paramString)
  {
    try
    {
      return paramClass.getDeclaredField(paramString);
    }
    catch (NoSuchFieldException localNoSuchFieldException)
    {
      paramClass = paramClass.getSuperclass();
      if (paramClass != null)
        return getDeclaredField(paramClass, paramString);
    }
    return null;
  }

  public static boolean classInstanceOf(Class paramClass1, Class paramClass2)
  {
    int i;
    if ((paramClass1 == null) || (paramClass2 == null))
      return false;
    if (paramClass1.equals(paramClass2))
      return true;
    Class[] arrayOfClass = paramClass1.getInterfaces();
    if (arrayOfClass != null)
      for (i = 0; i < arrayOfClass.length; ++i)
        if (classInstanceOf(arrayOfClass[i], paramClass2))
          return true;
    return classInstanceOf(paramClass1.getSuperclass(), paramClass2);
  }

  public static boolean isArray(Object paramObject)
  {
    return ((paramObject != null) && (paramObject.getClass().isArray()));
  }

  public static byte[] objectToByte(Object paramObject)
  {
    byte[] arrayOfByte = new byte[0];
    try
    {
      ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
      ObjectOutputStream localObjectOutputStream = new ObjectOutputStream(localByteArrayOutputStream);
      localObjectOutputStream.writeObject(paramObject);
      arrayOfByte = localByteArrayOutputStream.toByteArray();
      localByteArrayOutputStream.close();
      localObjectOutputStream.close();
    }
    catch (Exception localException)
    {
      System.out.println("translation" + localException.getMessage());
      localException.printStackTrace();
    }
    return arrayOfByte;
  }

  public static String getInstallHome()
  {
    String[] arrayOfString;
    int i;
    String str = System.getProperty("user.dir");
    if (str == null)
      return "d:\\opt\\FineReport_6.5\\";
    File localFile = new File(str);
    if ((localFile.exists()) && (localFile.isDirectory()))
    {
      arrayOfString = localFile.list();
      for (i = 0; i < arrayOfString.length; ++i)
        if (arrayOfString[i].indexOf(".exe") != -1)
          return localFile.getParent();
    }
    return "d:\\opt\\FineReport_6.5\\";
  }

  public static Properties getSystemEnvs()
    throws Exception
  {
    if (prop == null)
      prop = new Properties();
    else
      return prop;
    Process localProcess = null;
    if (OperatingSystem.isWindows())
      localProcess = Runtime.getRuntime().exec("cmd /c set");
    else
      return prop;
    BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(localProcess.getInputStream()));
    while (true)
    {
      String str1;
      int i;
      do
      {
        if ((str1 = localBufferedReader.readLine()) == null)
          break label118;
        i = str1.indexOf("=");
      }
      while (i <= -1);
      String str2 = str1.substring(0, i);
      String str3 = str1.substring(i + 1);
      prop.setProperty(str2, str3);
    }
    label118: return prop;
  }

  public static void zip(ZipOutputStream paramZipOutputStream, File paramFile, String paramString)
    throws Exception
  {
    if (paramFile.isDirectory())
    {
      localObject = paramFile.listFiles();
      paramZipOutputStream.putNextEntry(new ZipEntry(paramString + "/"));
      paramString = paramString + "/";
      i = 0;
      while (true)
      {
        if (i >= localObject.length)
          return;
        zip(paramZipOutputStream, localObject[i], paramString + localObject[i].getName());
        ++i;
      }
    }
    FRContext.getLogger().log(Level.INFO, "compress files" + paramString);
    paramZipOutputStream.putNextEntry(new ZipEntry(paramString));
    Object localObject = new FileInputStream(paramFile);
    int i = 0;
    byte[] arrayOfByte = new byte[1024];
    while ((i = ((FileInputStream)localObject).read(arrayOfByte)) > 0)
      paramZipOutputStream.write(arrayOfByte, 0, i);
    ((FileInputStream)localObject).close();
    paramZipOutputStream.flush();
  }

  public static String[] getAvailableFontFamilyNames4Report()
  {
    if (availableFontFamilyNames4Report == null)
    {
      String[] arrayOfString;
      int i;
      List localList = Arrays.asList(new String[] { "serif", "sansserif", "monospaced", "dialog", "dialoginput" });
      ArrayList localArrayList = new ArrayList();
      GraphicsEnvironment localGraphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
      if (localGraphicsEnvironment != null)
      {
        arrayOfString = localGraphicsEnvironment.getAvailableFontFamilyNames();
        for (i = 0; i < arrayOfString.length; ++i)
          if (localList.indexOf(arrayOfString[i].toLowerCase()) < 0)
            localArrayList.add(arrayOfString[i]);
      }
      availableFontFamilyNames4Report = (String[])localArrayList.toArray(new String[localArrayList.size()]);
    }
    return availableFontFamilyNames4Report;
  }

  public static void openWindowsFolder(String paramString)
  {
    if (!(OperatingSystem.isWindows()))
      return;
    File localFile = new File(paramString);
    if (!(localFile.exists()))
      return;
    if (localFile.isFile())
      try
      {
        Runtime.getRuntime().exec("explorer /select, " + localFile.getAbsolutePath());
      }
      catch (IOException localIOException1)
      {
      }
    else if (localFile.isDirectory())
      try
      {
        Runtime.getRuntime().exec("explorer " + localFile.getAbsolutePath());
      }
      catch (IOException localIOException2)
      {
      }
  }
}